package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.USERID;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.luoxiang.lib_common.utils.MmkvUtil;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.CertifyBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class RealAuditActivity extends BaseActivity {
    private EditText edit_name, edit_id;
    private TextView bt_finish;
    private ImageView icon_back;
    private String token;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        bt_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveRealAuditInfo();
            }
        });
    }

    private void saveRealAuditInfo() {
        getRequest();
        String name = edit_name.getText().toString();
        String id = edit_id.getText().toString();
        String userid = com.luoxiang.lib_common.utils.MmkvUtil.INSTANCE.decodeInt(USERID).toString();
        JSONObject jsonObject = new JSONObject();
        RequestBody requestBody = null;
        try {
            jsonObject.put("userId", userid);
            jsonObject.put("name", name);
            jsonObject.put("idCard", id);
            requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils2.getInstance().getApiService().saveRealAuditInfo(token, EXTRA, requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStraa", decryptStr);
                            Gson gson = new Gson();
                            CertifyBean bean = gson.fromJson(decryptStr, CertifyBean.class);
                            if (bean.getCode() == 0) {

                                if (bean != null) {
                                    if (bean.getData() != null) {
                                        if (bean.getData().getData() != null) {
                                            Intent intent = new Intent(RealAuditActivity.this, FacawebContentActivity.class);
                                            intent.putExtra("webcontent", bean.getData().getData().getCertifyUrl());
                                            intent.putExtra("cerid", bean.getData().getData().getCertifyId());
                                            intent.putExtra("namescard", edit_name.getText().toString());
                                            intent.putExtra("idcard", edit_id.getText().toString());
                                            startActivity(intent);
                                        }
                                    }
                                }


                            } else {
                                ToastUtils.showShort(bean.getMsg());
                            }

                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_realaudit);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        edit_id = findViewById(R.id.edit_id);
        edit_name = findViewById(R.id.edit_name);
        bt_finish = findViewById(R.id.bt_finish);
        icon_back = findViewById(R.id.icon_back);
    }
}
